package di.ass.input;

import di.ass.database.DatabaseHandler;
import di.ass.database.Food;
import di.ass.database.Formula;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.DatePicker;
import android.widget.TextView;
import di.ass.R;

public class AlertsAdd {

	public static void ShowFormulaAddedAlert(Context con) {
		DatabaseHandler db = new DatabaseHandler(con);

		String[] textAdd = null;
		if (db.getLanguageUse().equals("Việt Nam"))
			textAdd = con.getResources().getStringArray(R.array.add_dialog_vi);
		else if (db.getLanguageUse().equals("English")) {
			textAdd = con.getResources().getStringArray(R.array.add_dialog_eng);
		}

		AlertDialog.Builder builder = new AlertDialog.Builder(con);
		builder.setTitle(textAdd[0]);
		builder.setIcon(R.drawable.info_success);
		builder.setMessage(textAdd[1]);
		builder.setPositiveButton("OK", null);
		AlertDialog dialog = builder.create();
		dialog.show();
	}

	public static void ShowFoodAddedAlert(Context con) {
		DatabaseHandler db = new DatabaseHandler(con);

		String[] textAdd = null;
		if (db.getLanguageUse().equals("Việt Nam"))
			textAdd = con.getResources().getStringArray(R.array.add_dialog_vi);
		else if (db.getLanguageUse().equals("English")) {
			textAdd = con.getResources().getStringArray(R.array.add_dialog_eng);
		}
		AlertDialog.Builder builder = new AlertDialog.Builder(con);
		builder.setTitle(textAdd[0]);
		builder.setIcon(R.drawable.info_success);
		builder.setMessage(textAdd[2]);
		builder.setPositiveButton("OK", null);
		AlertDialog dialog = builder.create();
		dialog.show();
	}

	public static void ShowFoodNotAddAlert(Context con) {
		DatabaseHandler db = new DatabaseHandler(con);

		String[] textAdd = null;
		if (db.getLanguageUse().equals("Việt Nam"))
			textAdd = con.getResources().getStringArray(R.array.add_dialog_vi);
		else if (db.getLanguageUse().equals("English")) {
			textAdd = con.getResources().getStringArray(R.array.add_dialog_eng);
		}
		AlertDialog.Builder builder = new AlertDialog.Builder(con);
		builder.setTitle(textAdd[0]);
		builder.setIcon(R.drawable.info_fail);
		builder.setMessage(textAdd[3]);
		builder.setPositiveButton("OK", null);
		AlertDialog dialog = builder.create();
		dialog.show();
	}

	public static void ShowFoodEmptyAddAlert(Context con) {
		DatabaseHandler db = new DatabaseHandler(con);

		String[] textAdd = null;
		if (db.getLanguageUse().equals("Việt Nam"))
			textAdd = con.getResources().getStringArray(R.array.add_dialog_vi);
		else if (db.getLanguageUse().equals("English")) {
			textAdd = con.getResources().getStringArray(R.array.add_dialog_eng);
		}
		AlertDialog.Builder builder = new AlertDialog.Builder(con);
		builder.setTitle(textAdd[0]);
		builder.setIcon(R.drawable.info_fail);
		builder.setMessage(textAdd[4]);
		builder.setPositiveButton("OK", null);
		AlertDialog dialog = builder.create();
		dialog.show();
	}

	public static void ShowFormulaDup(final Context context,
			final String[] formula, final int formula_id) {
		DatabaseHandler db = new DatabaseHandler(context);

		String[] textAdd = null;
		if (db.getLanguageUse().equals("Việt Nam"))
			textAdd = context.getResources().getStringArray(
					R.array.add_dialog_vi);
		else if (db.getLanguageUse().equals("English")) {
			textAdd = context.getResources().getStringArray(
					R.array.add_dialog_eng);
		}
		AlertDialog.Builder builder = new AlertDialog.Builder(context);
		builder.setTitle(textAdd[0]);
		builder.setIcon(R.drawable.info);
		builder.setMessage(textAdd[5]);
		builder.setPositiveButton(textAdd[11], new OnClickListener() {

			@Override
			public void onClick(DialogInterface dialog, int which) {
				// TODO Auto-generated method stub
				Intent intentView = new Intent("di.ass.view.LISTFOOD");
				Bundle myBundle = new Bundle();
				myBundle.putInt("idFormula", formula_id);
				myBundle.putString("dateFormula", formula[3] + "/" + formula[2]
						+ "/" + formula[1]);
				myBundle.putString("nameFormula", formula[0]);
				intentView.putExtras(myBundle);
				context.startActivity(intentView);
			}
		});
		builder.setNegativeButton(textAdd[12], null);
		AlertDialog dialog = builder.create();
		dialog.show();
	}

	public static AlertDialog ShowAddFormulaDialog(final Context context,
			String[] oldFormula) {
		final DatabaseHandler db = new DatabaseHandler(context);

		String[] textAdd = null, textF = null;
		if (db.getLanguageUse().equals("Việt Nam")) {
			textAdd = context.getResources().getStringArray(
					R.array.add_dialog_vi);
			textF = context.getResources().getStringArray(R.array.textview_vi);
		} else if (db.getLanguageUse().equals("English")) {
			textAdd = context.getResources().getStringArray(
					R.array.add_dialog_eng);
			textF = context.getResources().getStringArray(R.array.textview_eng);
		}
		final AlertDialog.Builder builder = new AlertDialog.Builder(context);
		builder.setTitle(textAdd[6]);
		LayoutInflater layoutInflater = LayoutInflater.from(context);
		View view = layoutInflater.inflate(R.layout.input_formula, null);
		builder.setIcon(R.drawable.info);
		builder.setView(view);
		TextView textViewNameF = (TextView) view
				.findViewById(R.id.textViewNameF);
		textViewNameF.setText(textF[0]);

		TextView textViewDate = (TextView) view
				.findViewById(R.id.textViewDateF);
		textViewDate.setText(textF[1]);
		final TextView txtName = (TextView) view.findViewById(R.id.nameF);
		final DatePicker datePicker = (DatePicker) view
				.findViewById(R.id.datePicker1);
		txtName.setHint(textAdd[8] + " " + (db.getMaxIdFormula() + 1));
		builder.setPositiveButton(textAdd[9], new OnClickListener() {

			@Override
			public void onClick(DialogInterface dialog, int which) {
				Formula formula = new Formula();
				// TODO Auto-generated method stub

				if (txtName.getText().toString().equals("") == true)
					formula.setName(txtName.getHint().toString());
				else
					formula.setName(txtName.getText().toString());
				formula.setDate(datePicker.getYear() + "/"
						+ (datePicker.getMonth() + 1) + "/"
						+ datePicker.getDayOfMonth());
				if (db.addFormula(formula) == true)
					ShowFormulaAddedAlert(context);
				else {
					int formula_id = db.getIdFormula(formula);
					String[] old_formula = { txtName.getText().toString(),
							String.valueOf(datePicker.getDayOfMonth()),
							String.valueOf(datePicker.getMonth()),
							String.valueOf(datePicker.getYear()) };
					ShowFormulaDup(context, old_formula, formula_id);
				}
			}
		});
		builder.setNegativeButton(textAdd[10], null);
		return builder.create();
	}

	public static AlertDialog ShowAddFoodDialog(final Context context,
			final int formula_id) {
		DatabaseHandler db = new DatabaseHandler(context);

		String[] textAdd = null;
		String[] textFood = null;
		if (db.getLanguageUse().equals("Việt Nam")) {
			textAdd = context.getResources().getStringArray(
					R.array.add_dialog_vi);
			textFood = context.getResources().getStringArray(
					R.array.textview_vi);
		} else if (db.getLanguageUse().equals("English")) {
			textAdd = context.getResources().getStringArray(
					R.array.add_dialog_eng);
			textFood = context.getResources().getStringArray(
					R.array.textview_eng);
		}
		final AlertDialog.Builder builder = new AlertDialog.Builder(context);
		builder.setTitle(textAdd[7]);
		LayoutInflater layoutInflater = LayoutInflater.from(context);
		View view = layoutInflater.inflate(R.layout.input_food, null);
		builder.setIcon(R.drawable.info);
		builder.setView(view);

		TextView textViewNameF = (TextView) view
				.findViewById(R.id.textViewNameFood);
		textViewNameF.setText(textFood[2]);

		TextView textViewData = (TextView) view
				.findViewById(R.id.textViewQuanFood);
		textViewData.setText(textFood[3]);
		final TextView txtFoodName = (TextView) view
				.findViewById(R.id.editNameFood);
		final TextView txtFoodQuantity = (TextView) view
				.findViewById(R.id.editQuantity);
		final TextView txtFoodUnitary = (TextView) view
				.findViewById(R.id.editUnitary);
		txtFoodName.setHint(textFood[4]);
		txtFoodQuantity.setHint(textFood[5]);
		txtFoodUnitary.setHint(textFood[6]);
		builder.setPositiveButton(textAdd[9], new OnClickListener() {

			@Override
			public void onClick(DialogInterface dialog, int which) {
				// TODO Auto-generated method stub
				Food food = new Food();
				if (txtFoodName.getText().toString().equals(""))
					ShowFoodEmptyAddAlert(context);
				else {
					food.setName(txtFoodName.getText().toString());
					food.setId_formula(formula_id);
					food.setQuantity(txtFoodQuantity.getText().toString());
					food.setUnitary(txtFoodUnitary.getText().toString());
					DatabaseHandler db = new DatabaseHandler(context);
					if (db.addFood(food))
						ShowFoodAddedAlert(context);
					else
						ShowFoodNotAddAlert(context);
				}
			}
		});
		builder.setNegativeButton(textAdd[10], null);
		return builder.create();
	}

}
